package com.lanou.wuyou.arcmenu;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.RotateAnimation;
import android.view.animation.TranslateAnimation;

/**
 * Created by dllo on 17/3/8.
 */

public class ArcMenu extends ViewGroup implements View.OnClickListener {
    //定义菜单的位置,默认为右下
    private static Position position = Position.RIGHT_BOTTOM;
    //定义菜单的初始状态,默认为关闭
    private static Status status = Status.CLOSE;
    //定义菜单半径
    private int radius;
    private View mainView;

    @Override
    public void onClick(View v) {
        rotateMainView(v,0f,360f,300);
        toggleMenu(300);
    }

    private void toggleMenu(int duration) {
        //为menuItem设置平移和旋转动画
        int count = getChildCount();
        for (int j = 0; j < count - 1; j++) {
            final View childView = getChildAt(j + 1);
            //设置子按钮为可见的
            childView.setVisibility(VISIBLE);

            int cl = (int) (radius * Math.sin(Math.PI/2/(count - 2) * j));
            int ct = (int) (radius * Math.cos(Math.PI/2/(count - 2) * j));

            int xFlag = 1;
            int yFlag = 1;

            //如果在左上或者在左下
            if (position == Position.LEFT_TOP || position == Position.LEFT_BOTTOM){
                xFlag = -1;
            }
            //如果在左上或者右上
            if (position == Position.LEFT_TOP || position == Position.RIGHT_TOP){
                yFlag = -1;
            }

            //定义一个动画
            Animation tranAnimation = null;
            if (status == Status.CLOSE){
                //打开动画
                tranAnimation = new TranslateAnimation(xFlag * cl,0,yFlag * ct,0);
            }
            else{
                //关闭动画
                tranAnimation = new TranslateAnimation(0,xFlag * cl,0,yFlag * ct);
            }
            tranAnimation.setFillAfter(true);
            tranAnimation.setDuration(duration);

            //给动画设置监听
            tranAnimation.setAnimationListener(new Animation.AnimationListener() {
                @Override
                public void onAnimationStart(Animation animation) {

                }

                @Override
                public void onAnimationEnd(Animation animation) {

                    //如果菜单是关闭的
                    if (status == Status.CLOSE){
                        //子菜单隐藏
                        childView.setVisibility(GONE);
                    }
                }

                @Override
                public void onAnimationRepeat(Animation animation) {

                }
            });
            //设置旋转动画
            RotateAnimation rotateAnimation = new RotateAnimation(0,720,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
            rotateAnimation.setFillAfter(true);
            rotateAnimation.setDuration(duration);

            //添加动画
            AnimationSet animationSet = new AnimationSet(true);
            animationSet.addAnimation(rotateAnimation);
            animationSet.addAnimation(tranAnimation);
            childView.startAnimation(animationSet);
        }
        //切换菜单状态
        changeStatus();
    }

    private void changeStatus() {
        //如果菜单是打开的就关闭,如果是关闭的就打开
        status = (status == Status.CLOSE ? Status.OPEN : Status.CLOSE);
    }

    //设置主按钮的旋转动画
    private void rotateMainView(View v, float start, float end, int duration) {
        RotateAnimation rotateAnimation = new RotateAnimation(start,end,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
        //设置半径
        rotateAnimation.setDuration(duration);
        //旋转后停止
        rotateAnimation.setFillAfter(true);
        v.startAnimation(rotateAnimation);
    }

    public enum Position{
        LEFT_TOP,LEFT_BOTTOM,RIGHT_TOP,RIGHT_BOTTOM
    }
    public enum Status{
        OPEN,CLOSE
    }
    public ArcMenu(Context context) {
        this(context,null);
    }

    public ArcMenu(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public ArcMenu(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        //设置按钮半径,第一个参数单位,第二个参数长度,第三个参数获取屏幕参数
        radius = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,100,getResources().getDisplayMetrics());
        //获取自定义参数
        TypedArray array = context.getApplicationContext().obtainStyledAttributes(attrs,R.styleable.ArcMenu,defStyleAttr,0);
        int pos = array.getInt(R.styleable.ArcMenu_position,3);
        //判断菜单的位置
        switch (pos){
            case 0:
                position = Position.LEFT_TOP;
                break;
            case 1:
                position = Position.LEFT_BOTTOM;
                break;
            case 2:
                position = Position.RIGHT_TOP;
                break;
            case 3:
                position = Position.RIGHT_BOTTOM;
                break;
        }
        //设置菜单的半径
        radius = (int) array.getDimension(R.styleable.ArcMenu_radius, TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 100, getResources().getDisplayMetrics()));
        array.recycle();
    }

    //测量子按钮
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int count = getChildCount();
        for (int i = 0; i < count; i++) {
            measureChild(getChildAt(i),widthMeasureSpec,heightMeasureSpec);
        }
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {

        //判断菜单的位置
        if(changed){
            layoutIv();
            //子按钮的位置
            int count = getChildCount();
            for (int i = 0; i < count - 1; i++) {
                View child = getChildAt(i + 1);

                int cl = (int) (radius * Math.sin(Math.PI/2/(count - 2 ) * i));
                int ct = (int) (radius * Math.cos(Math.PI/2/(count - 2) * i));

                int cWidth = child.getMeasuredWidth();
                int cHeight = child.getMeasuredHeight();

                //如果菜单在左下或者右下
                if (position == Position.LEFT_BOTTOM || position == Position.RIGHT_BOTTOM){
                    ct = getMeasuredHeight() - cHeight - ct;
                }
                //如果菜单在右上或者右下
                if (position == Position.RIGHT_TOP || position == Position.RIGHT_BOTTOM){
                    cl = getMeasuredWidth() - cWidth - cl;
                }
                //子按钮的位置
                child.layout(cl,ct,cl + cWidth,ct + cHeight);
            }
        }
    }

    //主按钮
    private void layoutIv() {
        mainView = getChildAt(0);
        mainView.setOnClickListener(this);

        int l = 0;
        int t = 0;
        int width = mainView.getMeasuredWidth();
        int height = mainView.getMeasuredHeight();

        //判断位置
        switch (position){
            case LEFT_TOP:
                l = 0;
                t = 0;
                break;
            case LEFT_BOTTOM:
                l = 0;
                t = getMeasuredHeight() - height;
                break;
            case RIGHT_TOP:
                l = getMeasuredWidth() - width;
                t = 0;
                break;
            case RIGHT_BOTTOM:
                l = getMeasuredWidth() - width;
                t = getMeasuredHeight() - height;
                break;
        }
        //主按钮的位置
        mainView.layout(l,t,l + width,t + height);
    }
}
